import { Injectable } from '@nestjs/common';
import { PrismaService } from '@admin/common/service/prisma/prisma.service';
import { Response } from 'express';
import { exportTable } from '@admin/common/utils';
import { QueryJpSurnameDto, CreateJpSurnameDto, UpdateJpSurnameDto } from '../dto/index';
import { Prisma } from '@prismaClient';
import { isNotEmpty, isEmpty } from 'class-validator';
@Injectable()
export class JpSurnameService {
  constructor(private prisma: PrismaService) {}
  /**@description 查询姓氏管理所有 */
  async selectSurnameAll() {
    return this.prisma.jpSurname.findMany();
  }
  /**@description 分页查询姓氏管理列表 */
  async selectSurnameList(q: QueryJpSurnameDto) {
    const queryCondition: Prisma.JpSurnameWhereInput = {};
    if (isNotEmpty(q['name'])) {
      queryCondition.name = {
        contains: q.name
      };
    }
    if (isNotEmpty(q['avatar'])) {
      queryCondition.avatar = {
        equals: q.avatar
      };
    }
    if (isNotEmpty(q['way'])) {
      queryCondition.way = {
        contains: q.way
      };
    }
    if (isNotEmpty(q['ancestor'])) {
      queryCondition.ancestor = {
        contains: q.ancestor
      };
    }
    if (isNotEmpty(q['introduce'])) {
      queryCondition.introduce = {
        equals: q.introduce
      };
    }
    if (isNotEmpty(q['status'])) {
      queryCondition.status = {
        equals: q.status
      };
    }
    return {
      rows: await this.prisma.jpSurname.findMany({
        skip: (q.pageNum - 1) * q.pageSize,
        take: q.pageSize,
        where: queryCondition
      }),
      total: await this.prisma.jpSurname.count({
        where: queryCondition
      })
    };
  }
  /**@description 查询姓氏管理详情 */
  async selectSurnameById(id: number) {
    return this.prisma.jpSurname.findUnique({
      where: {
        id
      }
    });
  }
  /**@description 新增姓氏管理 */
  async addSurname(jpSurname: CreateJpSurnameDto) {
    if (isEmpty(jpSurname['name'])) {
      delete jpSurname['name'];
    }
    if (isEmpty(jpSurname['avatar'])) {
      delete jpSurname['avatar'];
    }
    if (isEmpty(jpSurname['way'])) {
      delete jpSurname['way'];
    }
    if (isEmpty(jpSurname['ancestor'])) {
      delete jpSurname['ancestor'];
    }
    if (isEmpty(jpSurname['introduce'])) {
      delete jpSurname['introduce'];
    }
    if (isEmpty(jpSurname['status'])) {
      delete jpSurname['status'];
    }
    if (isEmpty(jpSurname['createTime'])) {
      delete jpSurname['createTime'];
    }
    if (isEmpty(jpSurname['updateTime'])) {
      delete jpSurname['updateTime'];
    }
    return await this.prisma.jpSurname.create({
      data: jpSurname
    });
  }
  /**@description 修改姓氏管理 */
  async updateSurname(jpSurname: UpdateJpSurnameDto) {
    if (isEmpty(jpSurname['name'])) {
      delete jpSurname['name'];
    }
    if (isEmpty(jpSurname['avatar'])) {
      delete jpSurname['avatar'];
    }
    if (isEmpty(jpSurname['way'])) {
      delete jpSurname['way'];
    }
    if (isEmpty(jpSurname['ancestor'])) {
      delete jpSurname['ancestor'];
    }
    if (isEmpty(jpSurname['introduce'])) {
      delete jpSurname['introduce'];
    }
    if (isEmpty(jpSurname['status'])) {
      delete jpSurname['status'];
    }
    if (isEmpty(jpSurname['createTime'])) {
      delete jpSurname['createTime'];
    }
    if (isEmpty(jpSurname['updateTime'])) {
      delete jpSurname['updateTime'];
    }
    return await this.prisma.jpSurname.update({
      where: {
        id: jpSurname.id
      },
      data: jpSurname
    });
  }
  /**@description 批量删除姓氏管理 */
  async deleteSurnameByIds(ids: number[]) {
    return this.prisma.jpSurname.deleteMany({
      where: {
        id: {
          in: ids
        }
      }
    });
  }
  /**@description 单个删除姓氏管理 */
  async deleteSurnameById(id: number) {
    return this.prisma.jpSurname.delete({
      where: {
        id
      }
    });
  }
  /**@description 导出姓氏管理所有数据为xlsx */
  async exportSurname(res: Response) {
    const title = ['ID', '姓氏', '图腾', '得姓方式', '得姓始祖', '姓氏介绍', '状态(0禁用，1正常)', '注册时间', '修改时间'];
    const data = (await this.prisma.jpSurname.findMany()).map(v => Object.values(v));
    data.unshift(title);
    exportTable(data, res);
  }
}
